package allrobots.DispensingMachineThread;

import allrobots.DispensingMachine.DMOosAndOis;
import allrobots.DispensingMachine.DispensingMachinePosition;
import allrobots.pojo.SystemSocket;
import allrobots.sendMessages.Register.Registration;
import t10086.Data.Broadcast;
import t10086.Data.Map.TextCoordinate;
import t10086.Data.Robot.RobotType;
import t10086.Model.Messages.Message;
import t10086.Model.Messages.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Queue;

public class DispensingMachineThread implements Runnable {
    private int port;  // 目标端口号
    private String ipAddress;  // 目标 IP 地址
    public static boolean connectionStateSystem = false;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private Socket socket;
    public static final Queue<String> orders = new LinkedList<>();//保存订单
    private ServerSocket serverSocket;
    private InetAddress inetAddress;

    public DispensingMachineThread() throws IOException {
        this.port= SystemSocket.port;
        this.ipAddress=SystemSocket.ipAddress;
    }

    @Override
    public void run() {
        try {
            connect();  // 连接主系统；
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
//        new Thread(new DMFinishToSystem()).start();
        while (connectionStateSystem){
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("发药机还活着");
        }
    }

    //        try {
//            Thread.sleep(5000);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    private void connect() throws IOException {
        try {
            //发药机被连接监听端口
            try {
                serverSocket = new ServerSocket(0);
                inetAddress = InetAddress.getLocalHost();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            socket = new Socket(ipAddress, port);
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
            DMOosAndOis.ois = ois;
            DMOosAndOis.oos = oos;
            DMOosAndOis.socket=socket;
            connectionStateSystem = true;
            //发送需要连接发药机的端口和地址给系统（完成）
            Message toRobotMessage = new Message(MessageType.REGISTER, new Registration(RobotType.DELIVER, "132123", new InetSocketAddress(inetAddress.getHostAddress(), serverSocket.getLocalPort()), DispensingMachinePosition.position));
            //发送者
            toRobotMessage.setSender((InetSocketAddress) socket.getLocalSocketAddress());
//            System.out.println(socket.getLocalSocketAddress());
            //接收者
            toRobotMessage.setReceiver((InetSocketAddress) socket.getRemoteSocketAddress());
//            System.out.println(socket.getRemoteSocketAddress());
            oos.writeObject(toRobotMessage);
            System.out.println("发药机注册成功");
            oos.flush();
            System.out.println("DispensingMachine Connect System Success");
        } catch (Exception e) {
            e.printStackTrace();
            connectionStateSystem = false;
            oos.close();
            oos=null;
            ois.close();
            ois=null;
            socket=null;
        }
    }
}
